package com.ysstech.system.controller;


import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.common.entity.Organization;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.system.enums.SystemEnum;
import com.ysstech.system.service.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@Slf4j
@Api(tags = "机构管理api")
@RequestMapping(value = "/organization")
public class OrganizationController extends BaseController {

    @Autowired
    private OrganizationService organizationService;

    /**
     * 跳转机构信息页面
     *
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"system:organization"})
    @ApiOperation(value = "跳转机构管理页面")
    @YssLog(module = "机构管理", remark = "跳转机构管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "system/organization";
    }

    /**
     * 加载机构树信息
     *
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"system:organization"})
    @ApiOperation(value = "获取机构树信息")
    @YssLog(module = "机构管理", remark = "获取机构树信息")
    @RequestMapping(value = "/queryOrganizationTree")
    @ResponseBody
    public RestResult queryOrganizationTree() throws Exception {
        String orgId = redisCacheManager.getLoginUserInfo().getOrgId();
        return ResultGenerator.getSuccessResult("获取机构信息成功！", organizationService.queryOrganizationTree(orgId));
    }


    @YssPermissions(value = {"system:organization"})
    @ApiOperation(value = "获取角色页面机构的状态的值")
    @YssLog(module = "角色管理", remark = "获取角色页面机构的状态的值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        List<Dicvalue> type = dicValueService.getCacheDicValueList(SystemEnum.SYS_ORGANIZATION_TYPE.getCode());
        String orgId = redisCacheManager.getLoginUserInfo().getOrgId();
        List<TreeNode> listOrganization = organizationService.queryOrganizationTree(orgId);
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("listOrganization", listOrganization);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }

    /**
     * 获取一个机构的详细信息
     *
     * @param orgId
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"system:organization"})
    @ApiOperation(value = "获取机构信息")
    @YssLog(module = "机构管理", remark = "获取机构信息")
    @RequestMapping(value = "/queryOrganizationInfo")
    @ResponseBody
    public RestResult queryOrganizationInfo(String orgId) throws Exception {
        return ResultGenerator.getSuccessResult("获取机构信息成功！", organizationService.getById(orgId));
    }

    /**
     * 新增机构的方法
     *
     * @param organization
     * @return
     */
    @YssPermissions(value = {"system:organization:add"})
    @ApiOperation(value = "新增机构信息")
    @YssLog(module = "机构管理", remark = "新增机构信息")
    @RequestMapping(value = "/addOrganization")
    @ResponseBody
    public RestResult addOrganization(Organization organization) throws Exception {
        organization.setId(DateUtil.generateUUID());
        organization.setAddTime(LocalDateTime.now());
        organization.setStatus(1);
        organizationService.save(organization);
        redisCacheManager.removeCache(SystemEnum.YSS_ORGANIZATION.getCode());
        return ResultGenerator.getSuccessResult("新增机构信息成功！");
    }

    /**
     * 修改机构信息
     *
     * @return
     */
    @YssPermissions(value = {"system:organization:edit"})
    @ApiOperation(value = "修改机构信息")
    @YssLog(module = "机构管理", remark = "修改机构信息")
    @RequestMapping(value = "/editOrganization")
    @ResponseBody
    public RestResult editOrganization(Organization organization) throws Exception {
        if (organization == null || StringUtils.isBlank(organization.getId())) {
            throw new BusinessException("获取机构信息失败，请稍后重试！");
        }
        organizationService.updateById(organization);
        redisCacheManager.removeCache(SystemEnum.YSS_ORGANIZATION.getCode());
        return ResultGenerator.getSuccessResult("修改机构信息成功！");
    }

    /**
     * 删除机构信息
     *
     * @return
     */
    @YssPermissions(value = {"system:organization:del"})
    @ApiOperation(value = "删除机构信息")
    @YssLog(module = "机构管理", remark = "删除机构信息")
    @RequestMapping(value = "/delOrganization")
    @ResponseBody
    public RestResult delOrganization(String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException("获取删除机构信息失败，请稍后重试！");
        }
        List<String> list = organizationService.queryOrganizationIds(id); //所有下级机构
        organizationService.removeByIds(list);
        redisCacheManager.removeCache(SystemEnum.YSS_ORGANIZATION.getCode());
        return ResultGenerator.getSuccessResult("删除机构信息成功！");
    }

    /**
     * 读取机构文件的excle
     *
     * @param file
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"system:organization:readExcel"})
    @ApiOperation(value = "上传机构文件")
    @YssLog(module = "机构管理", remark = "上传机构文件")
    @RequestMapping(value = "/readExcel")
    @ResponseBody
    public RestResult readExcel(MultipartFile file) throws Exception {
        if (null == file || file.isEmpty()) {
            return ResultGenerator.getBusinessResult("获取文件信息为空，请检查文件后重新上传！");
        }
        organizationService.readExcel(file);
        return ResultGenerator.getSuccessResult("更新机构信息成功！");
    }

}
